<template>
  <div class="category-page">
    <!-- 分类列表部分 -->
    <el-row :gutter="20" class="category-list">
      <el-col :span="6" v-for="category in categories" :key="category.id">
        <el-card shadow="hover" @click="goToNotes(category)">
          <div class="category-name">{{ category.name }}</div>
          <div class="category-description">{{ category.description }}</div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getAction } from '@/api/manage';
import { Data } from '@/api/ajaxUrl.config';
import {useCategoryStore} from "@/stores/counter";

interface Category {
  id: number;
  name: string;
  description: string;
}

const categories = ref<Category[]>([]);

// 获取分类
const fetchCategories = () => {
  getAction(Data.category, '').then((response: any) => {
    categories.value = response.results;
  });
};

// 路由跳转，传递分类 ID
const categoryStore = useCategoryStore();
const router = useRouter();
const goToNotes = (category: object) => {
  categoryStore.setCategory(category)
  router.push({ name: 'note', });
};


onMounted(() => {
  fetchCategories();
});
</script>

<style scoped lang="less">
.category-page {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  text-align: center;
  height: 100%;
}

.category-list {
  margin-bottom: 20px;

}

.category-name {
  font-size: 18px;
  font-weight: bold;
  color: var(--color-text);

}

.category-description {
  margin-top: 10px;
  color: #888;

}

.el-card{
  margin-bottom: 20px;
  cursor: pointer;
  border-radius: 10px;
     :hover{
    color: #a8b1ff;

  }
}

/* 针对小屏设备 (手机) */
@media (max-width: 767px) {
  /* 小屏样式 */
  .category-page {
  padding: 5px;
  text-align: center;
  height: 100%;
}
}

</style>
